{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Cheat Sheet\n",
    "\n",
    "Basic cheatsheet for Python mostly based on the book written by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.\n",
    "\n",
    "## Read It\n",
    "\n",
    "- [Website](https://www.pythoncheatsheet.org)\n",
    "- [Github](https://github.com/wilfredinni/python-cheatsheet)\n",
    "- [PDF](https://github.com/wilfredinni/Python-cheatsheet/raw/master/python_cheat_sheet.pdf)\n",
    "- [Jupyter Notebook](https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks)\n",
    "\n",
    "## Flow Control\n",
    "\n",
    "### Comparison Operators\n",
    "\n",
    "| Operator | Meaning                  |\n",
    "| -------- | ------------------------ |\n",
    "| `==`     | Equal to                 |\n",
    "| `!=`     | Not equal to             |\n",
    "| `<`      | Less than                |\n",
    "| `>`      | Greater Than             |\n",
    "| `<=`     | Less than or Equal to    |\n",
    "| `>=`     | Greater than or Equal to |\n",
    "\n",
    "These operators evaluate to True or False depending on the values you give them.\n",
    "\n",
    "Examples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "42 == 42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "40 == 42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'hello' == 'hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'hello' == 'Hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'dog' != 'cat'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "42 == 42.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "42 == '42'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Boolean evaluation\n",
    "\n",
    "Never use `==` or `!=` operator to evaluate boolean operation. Use the `is` or `is not` operators,\n",
    "or use implicit boolean evaluation.\n",
    "\n",
    "NO (even if they are valid Python):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "True == True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "True != False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "YES (even if they are valid Python):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "True is True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "True is not False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These statements are equivalent:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "Python"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "if a is True:\n",
    "   pass\n",
    "if a is not False:\n",
    "   pass\n",
    "if a:\n",
    "   pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And these as well:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "attributes": {
     "classes": [
      "Python"
     ],
     "id": ""
    }
   },
   "outputs": [],
   "source": [
    "if a is False:\n",
    "   pass\n",
    "if a is not True:\n",
    "   pass\n",
    "if not a:\n",
    "   pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Boolean Operators\n",
    "\n",
    "There are three Boolean operators: and, or, and not.\n",
    "\n",
    "The *and* Operator’s *Truth* Table:\n",
    "\n",
    "| Expression      | Evaluates to |\n",
    "| --------------- | ------------ |\n",
    "| True and True   | True         |\n",
    "| True and False  | False        |\n",
    "| False and True  | False        |\n",
    "| False and False | False        |\n",
    "\n",
    "The *or* Operator’s *Truth* Table:\n",
    "\n",
    "| Expression     | Evaluates to |\n",
    "| -------------- | ------------ |\n",
    "| True or True   | True         |\n",
    "| True or False  | True         |\n",
    "| False or True  | True         |\n",
    "| False or False | False        |\n",
    "\n",
    "The *not* Operator’s *Truth* Table:\n",
    "\n",
    "| Expression | Evaluates to |\n",
    "| ---------- | ------------ |\n",
    "| not True   | False        |\n",
    "| not False  | True         |\n",
    "\n",
    "### Mixing Boolean and Comparison Operators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(4 < 5) and (5 < 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(4 < 5) and (9 < 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(1 == 2) or (2 == 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also use multiple Boolean operators in an expression, along with the comparison operators:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### if Statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if name == 'Alice':\n",
    "    print('Hi, Alice.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### else Statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = 'Bob'\n",
    "\n",
    "if name == 'Alice':\n",
    "    print('Hi, Alice.')\n",
    "else:\n",
    "    print('Hello, stranger.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### elif Statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = 'Bob'\n",
    "age = 5\n",
    "\n",
    "if name == 'Alice':\n",
    "    print('Hi, Alice.')\n",
    "elif age < 12:\n",
    "    print('You are not Alice, kiddo.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = 'Bob'\n",
    "age = 30\n",
    "\n",
    "if name == 'Alice':\n",
    "    print('Hi, Alice.')\n",
    "elif age < 12:\n",
    "    print('You are not Alice, kiddo.')\n",
    "else:\n",
    "    print('You are neither Alice nor a little kid.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### while Loop Statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spam = 0\n",
    "\n",
    "while spam < 5:\n",
    "    print('Hello, world.')\n",
    "    spam = spam + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### break Statements\n",
    "\n",
    " If the execution reaches a break statement, it immediately exits the while loop’s clause:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    print('Please type your name.')\n",
    "    name = input()\n",
    "    if name == 'your name':\n",
    "        break\n",
    "\n",
    "print('Thank you!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### continue Statements\n",
    "\n",
    "When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    print('Who are you?')\n",
    "    name = input()\n",
    "    if name != 'Joe':\n",
    "        continue\n",
    "    print('Hello, Joe. What is the password? (It is a fish.)')\n",
    "    password = input()\n",
    "    if password == 'swordfish':\n",
    "        break\n",
    "\n",
    "print('Access granted.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for Loops and the range() Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('My name is')\n",
    "for i in range(5):\n",
    "    print('Jimmy Five Times ({})'.format(str(i)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The *range()* function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0, 10, 2):\n",
    "   print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can even use a negative number for the step argument to make the for loop count down instead of up."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(5, -1, -1):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### For else statement\n",
    "\n",
    "This allows to specify a statement to execute in case of the full loop has been executed. Only\n",
    "useful when a `break` condition can occur in the loop:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in [1, 2, 3, 4, 5]:\n",
    "   if i == 3:\n",
    "       break\n",
    "else:\n",
    "   print(\"only executed when no item of the list is equal to 3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Importing Modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "for i in range(5):\n",
    "    print(random.randint(1, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random, sys, os, math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import *."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ending a Program with sys.exit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "while True:\n",
    "    print('Type exit to exit.')\n",
    "    response = input()\n",
    "    if response == 'exit':\n",
    "        sys.exit()\n",
    "    print('You typed {}.'.format(response))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
